import { validByField } from "@/utils/valid";
import {
  counterSchema,
  couponSchema,
  IProduct,
  packageBaseSchema,
  storeSchema,
  timeSchema,
} from "@/validator/package.valid";
import { ref } from "vue";
import { object } from "zod";

const usePackage = () => {
  const packageBaseModel = ref({
    name: {
      value: "",
      ruleMessage: "",
      valid: (value: string) => {
        packageBaseModel.value.name.ruleMessage = validByField(
          "name",
          value,
          packageBaseSchema.shape
        );
        return packageBaseModel.value.name.ruleMessage === "";
      },
    },
    price: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageBaseModel.value.price.ruleMessage = validByField(
          "price",
          value,
          packageBaseSchema.shape
        );
        return packageBaseModel.value.price.ruleMessage === "";
      },
    },
    actualPrice: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageBaseModel.value.actualPrice.ruleMessage = validByField(
          "actualPrice",
          value,
          packageBaseSchema.shape
        );
        return packageBaseModel.value.actualPrice.ruleMessage === "";
      },
    },
    points: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageBaseModel.value.points.ruleMessage = validByField(
          "points",
          value,
          packageBaseSchema.shape
        );
        return packageBaseModel.value.points.ruleMessage === "";
      },
    },
  });

  const packageStoreModel = ref({
    isUse: false,
    baseAmount: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageStoreModel.value.baseAmount.ruleMessage = validByField(
          "baseAmount",
          value,
          storeSchema.shape
        );
        return packageStoreModel.value.baseAmount.ruleMessage === "";
      },
    },
    giveAmount: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageStoreModel.value.giveAmount.ruleMessage = validByField(
          "giveAmount",
          value,
          storeSchema.shape
        );
        return packageStoreModel.value.giveAmount.ruleMessage === "";
      },
    },
    balanceAmount: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageStoreModel.value.balanceAmount.ruleMessage = validByField(
          "balanceAmount",
          value,
          storeSchema.shape
        );
        return packageStoreModel.value.balanceAmount.ruleMessage === "";
      },
    },
    discount: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageStoreModel.value.discount.ruleMessage = validByField(
          "discount",
          value,
          storeSchema.shape
        );
        return packageStoreModel.value.discount.ruleMessage === "";
      },
    },
    term: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageStoreModel.value.term.ruleMessage = validByField(
          "term",
          value,
          storeSchema.shape
        );
        return packageStoreModel.value.term.ruleMessage === "";
      },
    },
  });

  const packageCounterModel = ref({
    isUse: false,
    products: {
      value: [] as IProduct[],
      ruleMessage: "",
      valid: (value: IProduct[]) => {
        packageCounterModel.value.products.ruleMessage = validByField(
          "products",
          value,
          counterSchema.shape
        );
        return packageCounterModel.value.products.ruleMessage === "";
      },
    },
  });

  const packageTimeModel = ref({
    isUse: false,
    term: {
      value: 0,
      ruleMessage: "",
      valid: (value: number) => {
        packageTimeModel.value.term.ruleMessage = validByField(
          "term",
          value,
          timeSchema.shape
        );
        return packageTimeModel.value.term.ruleMessage === "";
      },
    },
  });

  const packageCouponModel = ref({
    coupon: {
      value: [] as string[],
      ruleMessage: "",
      valid: (value: string) => {
        packageCouponModel.value.coupon.ruleMessage = validByField(
          "coupon",
          value,
          couponSchema.shape
        );
        return packageCouponModel.value.coupon.ruleMessage === "";
      },
    },
  });

  const doValid = (value: Record<string, IFormModel>): boolean => {
    let b = true;
    const keys = Object.keys(value);
    keys.forEach((key) => {
      if (value[key].valid(value[key].value) === false) b = false;
    });
    return b;
  };
  const storeValid = () => {
    let b = true;
    const { isUse, ...obj } = packageStoreModel.value;
    if (isUse) b = doValid(obj);
    return b;
  };
  const counterValid = () => {
    let b = true;
    const { isUse, ...obj } = packageCounterModel.value;
    if (isUse) b = doValid(obj);
    return b;
  };
  const timeValid = () => {
    let b = true;
    const { isUse, ...obj } = packageTimeModel.value;
    if (isUse) b = doValid(obj);
    return b;
  };

  const packageValid = () => {
    let b = true;
    if (doValid(packageBaseModel.value) === false) b = false;
    if (storeValid() === false) b = false;
    if (counterValid() === false) b = false;
    if (timeValid() === false) b = false;
    packageCouponModel.value.coupon.value = [];
    packageCouponModel.value.coupon.ruleMessage = "";
    return b;
  };
  const doReset = (value: Record<string, IFormModel>) => {
    const keys = Object.keys(value);
    keys.forEach((key) => {
      if (typeof value[key].value === "number") value[key].value = 0;
      else if (typeof value[key].value === "string") value[key].value = "";
      else if (typeof value[key].value === "boolean") value[key].value = false;
      else if (Array.isArray(value[key].value)) value[key].value = [];
      else if (typeof value[key].value === "object") value[key].value = {};
      value[key].ruleMessage = "";
    });
  };
  const storeReset = () => {
    const { isUse, ...obj } = packageStoreModel.value;
    doReset(obj);
  };
  const counterReset = () => {
    const { isUse, ...obj } = packageCounterModel.value;
    doReset(obj);
  };
  const timeReset = () => {
    const { isUse, ...obj } = packageTimeModel.value;
    doReset(obj);
  };
  const packageReset = () => {
    doReset(packageBaseModel.value);
    storeReset();
    counterReset();
    timeReset();
  };

  return {
    packageBaseModel,
    packageStoreModel,
    packageCounterModel,
    packageTimeModel,
    packageCouponModel,

    packageValid,
    packageReset,
  };
};

export default usePackage;
